'use strict';
const express = require('express');
const router = express.Router();
const logger = require('log4js').getLogger("login");

const WechatUtil = require('../module/util/WechatUtil');

const MenuService = require('../service/auth/MenuService');
const UserService = require('../service/auth/UserService');
const CustomerService = require('../service/officialAccount/CustomerService');

const resUtil = require("../module/util/resUtil");

let userData = {
    city: "兰州市",
    country: "中国",
    groupid: 107,
    headimgurl: "http://wx.qlogo.cn/mmopen/ib3hzeKedibjibtkyxvFzreFLNVpmHfibtXNm5vU4WgMoJzOibJcHvZ7HBGzkiaL5XdanAQHGF7icka13VNWklhOBzxlYj0mHiaMgJvM/0",
    language: "zh_CN",
    nickname: "米城阳光",
    openid: "oGnbs1docHMLG-2ngSXH4e1FXe3c",
    province: "甘肃",
    remark: "",
    sex: 1,
    subscribe: 1,
    subscribe_time: 1502084937,
    tagid_list: [107]
};

/* GET home page. */
router.get('/', function(req, res, next) {
    if(req.session[req.query.publicId]){
        res.redirect('/view/index');
    }else{
        req.endType = "";
        // req.endType = global.util.endType(req.headers['user-agent']);

        let url = 'login';

        res.render(url, {
            path : "/"+url,
            endType : "",
            data : {},
            body : {},
            userData:{},
            cookie : req.cookies,
            redirect : req.query.redirect
        });
    }
});

router.post('/', function(req, res, next){
    let openid = req.body.openid;


    let wechatUtil = global.cache.officialAccount['b1a52d0d-c9f6-4f6a-a0f3-8312d154f17c'];

    wechatUtil.getCustomerInfo(openid).then(data => {
        req.session.userData = data;
        res.send(resUtil.success({data:data}));
    }, err => {
        res.send(resUtil.error({
            message : err.errmsg
        }));
    });
});

router.get('/logout', function(req, res, next){
    req.session.destroy(function(err) {
        res.redirect(global.config.path.contextPath + '/view/index');
    });
});

module.exports = router;
